package com.cti.ftpservice.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.cti.ftpservice.dao.FtpFileDao;
import com.cti.ftpservice.ftp.DateHelper;
import com.cti.ftpservice.model.onix.FtpFileInfo;
import com.cti.ftpservice.vo.PageViewVO;
import com.cti.ftpservice.vo.ParameterVo;


@Repository("ftpFileDao")
public class FtpFileDaoImpl extends BaseDao implements FtpFileDao {

	@Override
	public void addFtpFile(FtpFileInfo ftpFileInfo) {
		addObj(ftpFileInfo);
	}

	@Override
	public void deleteFtpFile(FtpFileInfo ftpFileInfo) {
		deleteObj(ftpFileInfo);
	}

	@Override
	public void addOrUpdateFtpFileInfo(FtpFileInfo ftpFileInfo) {
		addOrUpdateObj(ftpFileInfo);
	}

	@Override
	public FtpFileInfo getFileInfoById(String ftpFileId) {
		return getModel(FtpFileInfo.class, ftpFileId);
	}
	
	public FtpFileInfo getFileInfoBylocalFtpFilePath(String localFtpFilePath) {
		String hql="from "+FtpFileInfo.class.getName()+" t where t.localFtpFilePath=:localFtpFilePath";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		mapParams.put("localFtpFilePath", localFtpFilePath);
		return getSingleObject(hql,mapParams);
	}
	
	public FtpFileInfo getFileInfoByFileName(String fileName) {
		String hql="from "+FtpFileInfo.class.getName()+" t where t.fileName=:fileName";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		mapParams.put("fileName", fileName);
		return getSingleObject(hql,mapParams);
	}

	@Override
	public List<FtpFileInfo> getFtpFileInfosByApprovedHuman(
			Boolean approvedByHuman) {
		String hql = "from " + FtpFileInfo.class.getName() + " t where 1=1";
		if (approvedByHuman != null) {
			hql += " and t.approvedByHuman=:approvedByHuman";
			Map<String, Object> mapParams = new HashMap<String, Object>();
			mapParams.put("approvedByHuman", approvedByHuman);
			return getListObject(hql, mapParams);
		} else {
			return getListObject(hql);
		}
	}

	@Override
	public List<FtpFileInfo> getFtpFileInfosByFtpServiceIdAndApprovedHuman(
			String ftpServiceId, Boolean approvedByHuman) {
		String hql = "from " + FtpFileInfo.class.getName() + " t where 1=1";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		if (approvedByHuman != null) {
			hql += " and t.approvedByHuman=:approvedByHuman";
			mapParams.put("approvedByHuman", approvedByHuman);
		}
		if (!StringUtils.isBlank(ftpServiceId)) {
			hql += " and t.ftpService.id=:ftpServiceId";
			mapParams.put("ftpServiceId", ftpServiceId);
		}
		return getListObject(hql, mapParams);
	}

	@Override
	public PageViewVO<FtpFileInfo> getFtpFileInfoViewVoByParams(ParameterVo[] paramVos,int pageIndex,int pageSize) {
		String hql = "from " + FtpFileInfo.class.getName() + " t where 1=1";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		if (paramVos != null && paramVos.length > 0) {
			for (ParameterVo vo : paramVos) {
				switch (vo.getParameterName()) {
					case "approvedByHuman": {
						hql += " and t.approvedByHuman=:approvedByHuman";
						mapParams.put("approvedByHuman", (boolean)vo.getParameterValue());
					}break;
					case "ftpServiceType": {
						hql += " and t.ftpService.ftpServiceType=:ftpServiceType";
						mapParams.put("ftpServiceType", vo.getParameterValue().toString());
					}break;
					case "ftpServiceId": {
						hql += " and t.ftpService.id=:ftpServiceId";
						mapParams.put("ftpServiceId", vo.getParameterValue().toString());
					}break;
					case "extension": {
						hql += " and t.extension=:extension";
						mapParams.put("extension", vo.getParameterValue().toString());
					}break;
					case "satrtTime":{
						hql+=" and t.createDate>=:satrtTime";
						Date startData=DateHelper.stringToDate(vo.getParameterValue().toString(),"yyyy-MM-dd");
						mapParams.put("satrtTime",startData );
					}break;
					case "endTime":{
						hql+=" and t.createDate<=:endTime";
						Date startData=DateHelper.stringToDate(vo.getParameterValue().toString(),"yyyy-MM-dd");
						mapParams.put("endTime",startData );
					}break;
					case "fileName":{
						hql+=" and t.fileName like :fileName";
						mapParams.put("fileName",vo.getParameterValue().toString()+'%' );
					}break;
				}
			}
		}
		return getPageViewVO(hql, mapParams, pageIndex, pageSize);
	}

	


	

	@Override
	public List<FtpFileInfo> getFtpDownloadFileViewVoByFtpServiceIdAndDownloadPath(String downloadPath) {
		String hql = " from " + FtpFileInfo.class.getName() + " t where 1=1";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		if(!StringUtils.isBlank(downloadPath)){
			hql+=" and t.localFtpFilePath like :localFtpFilePath";
			mapParams.put("localFtpFilePath", downloadPath+"%");
		}
		return getListObject(hql,mapParams);
	}

	@Override
	public List<String> getFtpFileInfoExtensions() {
		String hql = "select distinct t.extension  from " + FtpFileInfo.class.getName()+" t";
		return getListObject(hql);
	}

	@Override
	public List<FtpFileInfo> getUnDealFileInfosByFileType(String fileType) {
		String hql = " from " + FtpFileInfo.class.getName() + " t where t.flieType=:fileType and t.parsed=:parsed";
		Map<String, Object> mapParams = new HashMap<String, Object>();
		mapParams.put("fileType", fileType);
		mapParams.put("parsed", false);
		return getListObject(hql,mapParams);
	}

	@Override
	public void clearSession() {
		clearCache();
	}
}
